package io.renren.modules.api.controller;

import io.renren.common.utils.ConstantUtils;
import io.renren.common.utils.R;
import io.renren.modules.user.service.UserinfosService;
import io.renren.modules.vipuser.entity.VipinfosEntity;
import io.renren.modules.vipuser.service.VipinfosService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Title: stf
 * @Description: java类作用描述
 * @Author: 杨帅
 * @CreateDate: 2018/10/31 17:37
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/vipuser")
public class ApiVipUserController {
    @Autowired
    private VipinfosService  vipinfosService;

    /**
     * 获取Vip基本信息
     */
    @GetMapping("/getVipUserInfo")
    public R getUserInfo(HttpServletRequest request){
        String userInfoId = (String)request.getAttribute(ConstantUtils.USER_KEY);
        if (StringUtils.isNotBlank(userInfoId)){
            VipinfosEntity vipinfosEntity = vipinfosService.queryExpirationTime(userInfoId);
            if (vipinfosEntity == null){
                return R.error("您当前不是会员");
            }
            //判断会员是否到期
            Date now = new Date();
            if (now.compareTo(vipinfosEntity.getExpirationtime()) > 0){
                return R.ok("您的会员已过期").put("data",vipinfosEntity);
            }
            return R.ok().put("data",vipinfosEntity);
        }
        return R.error("token获取失败");
    }

}
